//
//  BN_FeedBackViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-6-11.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_FeedBackViewController.h"

@interface BN_FeedBackViewController ()

@property (weak, nonatomic) IBOutlet CPTextViewPlaceholder *contentInfoTextView;

@end

@implementation BN_FeedBackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // UI
    self.tabBarController.tabBar.hidden  = YES;
    if (MAIN_VERSION >= 7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // UI
    self.title = @"意见反馈";
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVC)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submitAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    // TextView
    self.contentInfoTextView.placeholder = @"  写点什么吧...";
    [self.contentInfoTextView becomeFirstResponder];
    switch (MAIN_VERSION) {
        case 6:
            self.contentInfoTextView.frame = CGRectMake(0, 20, SCREEN_WIDTH, 140);
            break;
        default:
            break;
    }
}

#pragma mark Action
// 返回上级视图
- (void)backRootVC {
    [self.navigationController popViewControllerAnimated:YES];
}

// 提交信息
- (void)submitAction {
    // code...
    if (self.contentInfoTextView.text.length > 0 && ![self.contentInfoTextView.text isEqualToString:@"  写点什么吧..."]) {
        [self requestNetData];
    }else {
        [LZCHINT title:@"请输入您想说的内容" type:LZCInfo duration:2 places:HINT_PLACES callBack:NULL];
    }
}

- (void)requestNetData {
    userInfo = [BN_UserHelper unArchiverAccountInfo];
    NSString *beforeStr = NET_IF_USER_INFO_FEED_SUBMIT;
    NSString *lastStr = [NSString stringWithFormat:@"?account=%@&password=%@&feedSubj=%@&feedCont=%@",userInfo.account,userInfo.password,self.contentInfoTextView.text,self.contentInfoTextView.text];
    NSString *entiretyStr = FORMAT_STR([beforeStr stringByAppendingString:lastStr]);
    entiretyStr = [entiretyStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *feedbackUrl = [NSURL URLWithString:entiretyStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:feedbackUrl];
    [request setHTTPMethod:@"GET"];
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:request queue:operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        // code...
        dispatch_async(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:data
                                                                       options:NSJSONReadingMutableLeaves
                                                                         error:&error];
            NSString *errorCode = [NSString stringWithFormat:@"%@",resultDict[@"errorCode"]];
            if ([errorCode isEqualToString:@"0"]) {
                [LZCHINT title:@"您的意见发送成功" type:LZCSuccess duration:2 places:HINT_PLACES callBack:NULL];
                [self.navigationController popViewControllerAnimated:YES];
            }
        });

    }];
}

@end
